home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17204 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: ix.netcom.com!als-il2-02
  2. From: stefmit@ix.netcom.com
  3. Newsgroups: comp.lang.c++
  4. Subject: [Q] Expression tree evaluation
  5. Date: 13 Apr 1996 22:07:38 GMT
  6. Organization: Netcom
  7. Message-ID: <4kp8ja$s6p@cloner2.ix.netcom.com>
  8. NNTP-Posting-Host: als-il2-02.ix.netcom.com
  9. X-NETCOM-Date: Sat Apr 13  3:07:38 PM PDT 1996
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. Could anybody tell me what's wrong with this (I am surely missing something):
  13.  
  14. float ExprTree :: evaluate () const
  15. {    if (root != 0)
  16.         evaluateSub (root); }
  17.  
  18. float ExprTree :: evaluateSub (ExprTreeNode *p) const
  19. {    float result, l, r;
  20.     if (isdigit (p -> element))
  21.         result = ((p -> element) - '0');
  22.     else    {    l = evaluateSub (p -> left);
  23.             r = evaluateSub (p -> right); }
  24.     switch (p -> element) {
  25.         case '+' : result = l+r; break;
  26.         case '-' : result = l-r; break;
  27.         case '*' : result = l*r; break;
  28.         case '/' : result = l/r; break;
  29.         default : break;}
  30.     return (result); }
  31.  
  32. I am obviously trying to evaluate an expression, I "watched" l, r and result 
  33. and they show correct values (while stepping through the program), but the 
  34. program crashes with "stack underflow". Could anybody help, please? Thanks.
  35.